// pages/merchant/merchant.js
const time = require('../../utils/util.js')

var app = getApp()

Page({

  /**
   * 页面的初始数据
   */
  data: {
    result: ['a'],
    show: true,
    dateTime: {
      show: false,
      minDate: new Date().getTime(),
      currentDate: ''
    },
    formatter (type,value) {
      if(type === 'year') {
        return `${value}年`
      } else if(type === 'month') {
        return `${value}月`
      } else {
        return value
      }
    },
    startAt: {
      show: false,
      minHour: 8,
      maxHour: 23,
      hour: 0,
      minute: 0,
      currentDate: '08:00'
    },
    endAt: {
      show: false,
      minHour: 8,
      maxHour: 23,
      currentDate: '23:00'
    },
    currentDate: '',
    sjbj: {
      push_type: 1,
      service_name: '',
      service_at: '',
      work_at: '',
      days_project: '',
      price: '',
      required_number:'',
      address: '',
      mark: '',
      images: ["/images/upload.png", "/images/upload.png", "/images/upload.png", "/images/upload.png"]
    }
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function(options) {

  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function() {

  },

  /**
   * 绑定输入值
   */
  servValue: function(e) {
    this.setData({
      'sjbj.service_name': e.detail.value
    })
  },

  daysValue: function(e) {
    this.setData({
      'sjbj.days_project': e.detail.value
    })
  },

  moneyValue: function(e) {
    this.setData({
      'sjbj.price': e.detail.value
    })
  },

  peopleValue: function(e) {
    this.setData({
      'sjbj.required_number': e.detail.value
    })
  },

  addValue: function(e) {
    this.setData({
      'sjbj.address': e.detail.value
    })
  },

  workValue: function(e) {
    this.setData({
      'sjbj.work_at': e.detail.value
    })
  },

  remarkValue: function(e) {
    this.setData({
      'sjbj.mark': e.detail.value
    })
  },

  /**
   * 
   */
  tabChange: function(e) {
    let that = this
    that.setData({
      'sjbj.push_type': e.target.dataset.index,
      'sjbj.required_number': '',
      'sjbj.price': '',
      'sjbj.days_project': ''
    })
  },

  paymentClick: function() {
    let that = this,
      mapp = that.data,
      imgArr = [],
      data,trade_no,i,jsonArr

    if(mapp.sjbj.service_name == '') {
      wx.showToast({title: '填写服务名称'})
      return false
    }
    
    if(mapp.sjbj.service_at == '') {
      wx.showToast({title: '选择开始时间'})
      return false
    }

    if(mapp.sjbj.work_at == '') {
      wx.showToast({title: '填写工作时间'})
      return false
    }

    if(mapp.sjbj.days_project == '') {
      wx.showToast({title: '填写用工天数'})
      return false
    }

    if(mapp.sjbj.price == '') {
      wx.showToast({title: '填写雇佣金额'})
      return false
    }

    if (mapp.sjbj.required_number == '') {
      wx.showToast({title: '填写用工人数'})
      return false
    }

    if(mapp.sjbj.address == '') {
      wx.showToast({title: '填写工作地点'})
      return false
    }

    if(mapp.sjbj.mark == '') {
      wx.showToast({title: '请填写备注'})
      return false
    }

    wx.showLoading({
      mask: true,
      title: app.globalData.loadText,
    })

    app.getAccessToken(app.getPageUrl(getCurrentPages())).then(function(accessToken) {
      wx.request({
        url: app.globalData.hostAPI + '/v1/wxpay/push?access_token=' + accessToken,
        method: 'POST',
        data: {
          push_type: mapp.sjbj.push_type,
          service_name: mapp.sjbj.service_name,
          service_at: mapp.sjbj.service_at,
          work_at: mapp.sjbj.work_at,
          days_project: mapp.sjbj.days_project,
          price: mapp.sjbj.price,
          required_number: mapp.sjbj.required_number,
          address: mapp.sjbj.address,
          mark: mapp.sjbj.mark,
          images: JSON.stringify(mapp.sjbj.images)
        },
        header: {
          "content-type": "application/x-www-form-urlencoded"
        },
        success: res => {
          data = res.data.data
          if(res.data.status === 1) {
            wx.hideLoading()
            if (data.appId) {
              wx.requestPayment({
                timeStamp: data.timeStamp,
                nonceStr: data.nonceStr,
                package: data.package,
                signType: data.signType,
                paySign: data.paySign,
                success: res => {
                  wx.showModal({
                    title: '提示',
                    content: '支付成功',
                    success: res1 => {
                      if(res1.confirm) {
                        wx.redirectTo({
                          url: '/pages/success/success',
                        })
                      }
                    }
                  })
                }
              })
            } else {
              wx.showModal({
                title: '提示',
                content: res.data.msg,
                success: res2 => {
                  if(res2.confirm) {
                    wx.redirectTo({
                      url: '/pages/success/success',
                    })
                  }
                }
              })
            }
          }
        }
      })
    })
  },

  /**
   *
   */
  upLoad: function(e) {
    let that = this,
        bs = e.currentTarget.dataset.bs,
        imgArr = [],
        data, tempFilePaths
    app.getAccessToken(app.getPageUrl(getCurrentPages())).then(function (accessToken) {
      wx.chooseImage({
        count: 1,
        success: function (res) {
          tempFilePaths = res.tempFilePaths
          wx.uploadFile({
            url: app.globalData.hostAPI + '/v1/upload/index?access_token=' + accessToken,
            filePath: tempFilePaths[0],
            name: 'file',
            success: res => {
              data = JSON.parse(res.data)
              if(data.code === 1) {
                that.data.sjbj.images[bs] = data.data.url
                that.setData({
                  'sjbj.images': that.data.sjbj.images
                })
              }
            }
          })
        },
      })
    })
  },

  /**
   * service_at
   */
  showDatePick: function() {
    this.setData({
      'dateTime.show': true
    })
  },

  cancelStratTime: function () {
    this.setData({
      'dateTime.show': false
    })
  },

  dateInput: function(e) {
    let time = e.detail.getValues()
    this.setData({
      'dateTime.currentDate': time[0] + '-' + time[1] + '-' + time[2]
    })
  },

  cfmDate: function(e) {
    this.setData({
      'dateTime.show': false,
      'sjbj.service_at': time.formatTimeTwo(new Date(e.detail), 'Y-M-D'),
      'dateTime.currentDate': time.formatTimeTwo(new Date(e.detail),'Y-M-D')
    })
  },

  /**
   * start_at
   */
  showStartPick: function() {
    this.setData({
      'startAt.show': true
    })
  },

  closeStartPick: function () {
    this.setData({
      'startAt.show': false
    })
  },

  changeStartTime: function (e) {
    let time = e.detail.getValues()
    this.setData({
      'startAt.currentDate': time[0] + ':' + time[1],
    })
  },

  cfmStartTime: function(e) {
    this.setData({
      'startAt.show': false,
      'endAt.show': true,
      'endAt.minHour': e.detail.split(':')[0],
      'currentDate': this.data.startAt.currentDate + '-'
    })
  },

  /**
   * end_at
   */
  changeEndTime: function(e) {
    let time = e.detail.getValues()
    this.setData({
      'endAt.currentDate': time[0] + ':' + time[1],
    })
  },

  closeEndTime: function() {
    this.setData({
      'endAt.show': false
    })
  },

  cfmEndTime: function(e) {
    this.setData({
      'endAt.show': false,
      'currentDate': this.data.startAt.currentDate + '-' + this.data.endAt.currentDate,
      'sjbj.work_at': this.data.startAt.currentDate + '-' + this.data.endAt.currentDate
    })
  }
})
